package com.anolesoft.yr.mbus.prod.model.service.impl;

import com.anolesoft.yr.mbus.prod.base.dto.ProdBaseServiceSaveDTO;
import com.anolesoft.yr.mbus.prod.base.dto.ProdBaseServiceUpdateDTO;
import com.anolesoft.yr.mbus.prod.base.mapper.ProdBaseServiceMapper;
import com.anolesoft.yr.mbus.prod.base.mapper.ProdBaseServiceValueMapper;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceQueryVO;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceValueQueryVO;
import com.anolesoft.yr.mbus.prod.model.dto.*;
import com.anolesoft.yr.mbus.prod.model.entity.ProdProduction;
import com.anolesoft.yr.mbus.prod.model.mapper.ProdProductionMapper;
import com.anolesoft.yr.mbus.prod.model.mapper.ProdServiceMapper;
import com.anolesoft.yr.mbus.prod.model.mapper.ProdServiceValueMapper;
import com.anolesoft.yr.mbus.prod.model.service.ProdProductionService;
import com.anolesoft.yr.mbus.prod.model.vo.ProdProductionQueryVO;
import com.anolesoft.yr.mbus.prod.model.vo.ProdServiceQueryVO;
import com.anolesoft.yr.mbus.prod.model.vo.ProdServiceValueQueryVO;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import com.anolesoft.yr.mbus.util.StrCP;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeoutException;

@Service
public class ProdProductionServiceImpl implements ProdProductionService {

    private static final String CODE_EXIST = "CODE_EXIST";
    private static final String NAME_EXIST = "NAME_EXIST";
    private static final String SUCCESS = "SUCCESS";
    private static final String EXCEPTION = "EXCEPTION";

    @Autowired
    ProdProductionMapper prodProductionMapper;
    @Autowired
    ProdServiceMapper prodServiceMapper;
    @Autowired
    ProdServiceValueMapper prodServiceValueMapper;
    @Autowired
    ProdBaseServiceMapper prodBaseServiceMapper;
    @Autowired
    ProdBaseServiceValueMapper prodBaseServiceValueMapper;

    //新增产品信息表
    @Override
    @Transactional
    public String saveProdProduction(ProdProductionSaveDTO prodProductionSaveDTO) throws Exception {
        try {
            int count = 0;
            count = prodProductionMapper.queryProdProductionByCode(prodProductionSaveDTO.getProdCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            count = prodProductionMapper.queryProdProductionByName(prodProductionSaveDTO.getProdName());
            if (count > 0) {
                return NAME_EXIST;
            }

            List<ProdBaseServiceSaveDTO> prodBaseServiceSaveDTOList = prodProductionSaveDTO.getProdBaseServiceSaveDTO();
            for(int i=0; i<prodBaseServiceSaveDTOList.size(); i++){
                ProdBaseServiceSaveDTO prodBaseServiceSaveDTO = prodBaseServiceSaveDTOList.get(i);

                ProdBaseServiceQueryVO prodBaseServiceQueryVO = prodBaseServiceMapper.queryProdBaseServiceByUid2(prodBaseServiceSaveDTO.getObjuid());
                String prodProdServiceUid = StrCP.createObjuid32(UUID.randomUUID().toString());
                ProdServiceSaveDTO prodServiceSaveDTO = new ProdServiceSaveDTO();
                prodServiceSaveDTO.setObjuid(prodProdServiceUid);
                prodServiceSaveDTO.setFacCode("");
                prodServiceSaveDTO.setProdProductionUid(prodProductionSaveDTO.getObjuid());
                prodServiceSaveDTO.setProdBaseServiceUid(prodBaseServiceQueryVO.getObjuid());
                //prodServiceSaveDTO.setServiceNo(prodBaseServiceQueryVO.getBaseServiceNo());
                prodServiceSaveDTO.setServiceCode(prodBaseServiceQueryVO.getBaseServiceCode());
                prodServiceSaveDTO.setServiceName(prodBaseServiceQueryVO.getBaseServiceName());
                prodServiceSaveDTO.setImportType(prodBaseServiceQueryVO.getImportType());
                prodServiceSaveDTO.setIsTimeliness(new Integer(prodBaseServiceQueryVO.getIsTimeliness()));
                prodServiceSaveDTO.setRemark(prodBaseServiceQueryVO.getRemark());
                prodServiceSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
                prodServiceSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
                prodServiceSaveDTO.setDelFlag(0);

                List prodBaseServiceValueQueryVOList = prodBaseServiceValueMapper.queryProdBaseServiceValueByUid2(prodBaseServiceQueryVO.getObjuid());
                for(int j=0; j<prodBaseServiceValueQueryVOList.size(); j++){
                    ProdBaseServiceValueQueryVO prodBaseServiceValueQueryVO = (ProdBaseServiceValueQueryVO) prodBaseServiceValueQueryVOList.get(j);

                    String prodProdServiceValueUid = StrCP.createObjuid32(UUID.randomUUID().toString());
                    ProdServiceValueSaveDTO prodServiceValueSaveDTO = new ProdServiceValueSaveDTO();
                    prodServiceValueSaveDTO.setObjuid(prodProdServiceValueUid);
                    prodServiceValueSaveDTO.setFacCode("");
                    prodServiceValueSaveDTO.setProdProductionUid(prodProductionSaveDTO.getObjuid());
                    prodServiceValueSaveDTO.setProdServiceUid(prodProdServiceUid);
                    prodServiceValueSaveDTO.setProdBaseServiceValueUid(prodBaseServiceValueQueryVO.getObjuid());
                    prodServiceValueSaveDTO.setValue(prodBaseServiceValueQueryVO.getUpdateValue());
                    prodServiceValueSaveDTO.setIsDefault(new Integer(prodBaseServiceValueQueryVO.getUpdateIsDefault()));
                    prodServiceValueSaveDTO.setIsHoliday(new Integer(prodBaseServiceValueQueryVO.getUpdateIsHoliday()));
                    prodServiceValueSaveDTO.setAcceptTime(prodBaseServiceValueQueryVO.getUpdateAcceptTime());
                    prodServiceValueSaveDTO.setReplyTime(prodBaseServiceValueQueryVO.getUpdateReplyTime());
                    prodServiceValueSaveDTO.setDeliveryTime(prodBaseServiceValueQueryVO.getUpdateDeliveryTime());
                    prodServiceValueSaveDTO.setArrivalTime(prodBaseServiceValueQueryVO.getUpdateArrivalTime());
                    prodServiceValueSaveDTO.setRemark(prodBaseServiceValueQueryVO.getUpdateRemark());
                    prodServiceValueSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
                    prodServiceValueSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
                    prodServiceValueSaveDTO.setDelFlag(0);

                    prodServiceValueMapper.saveProdServiceValue(prodServiceValueSaveDTO);
                }

                prodServiceMapper.saveProdService(prodServiceSaveDTO);
            }

            int rows = prodProductionMapper.saveProdProduction(prodProductionSaveDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //修改产品信息表
    @Override
    @Transactional
    public String updateProdProduction(ProdProductionUpdateDTO prodProductionUpdateDTO) throws Exception {
        try {
            ProdProduction prodProduction = prodProductionMapper.queryProdProductionByUid2(prodProductionUpdateDTO.getObjuid());
            int count = 0;

            count = prodProductionMapper.query2ProdProductionByCode(prodProductionUpdateDTO.getProdTypeUid(), prodProductionUpdateDTO.getProdCode(), prodProduction.getProdCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            count = prodProductionMapper.query2ProdProductionByName(prodProductionUpdateDTO.getProdTypeUid(), prodProductionUpdateDTO.getProdName(), prodProduction.getProdName());
            if (count > 0) {
                return NAME_EXIST;
            }

            List prodServiceDeleteDTOList = Lists.newArrayList();
            ProdServiceDeleteDTO prodServiceDeleteDTO = null;
            List<ProdBaseServiceUpdateDTO> prodBaseServiceUpdateDTOList = prodProductionUpdateDTO.getProdBaseServiceUpdateDTO();
            for(int i=0; i<prodBaseServiceUpdateDTOList.size(); i++){
                ProdBaseServiceUpdateDTO prodBaseServiceUpdateDTO = prodBaseServiceUpdateDTOList.get(i);

                if("".equals(prodBaseServiceUpdateDTO.getUpdateObjuid())){
                    ProdBaseServiceQueryVO prodBaseServiceQueryVO = prodBaseServiceMapper.queryProdBaseServiceByUid2(prodBaseServiceUpdateDTO.getObjuid());

                    String prodProdServiceUid = StrCP.createObjuid32(UUID.randomUUID().toString());
                    ProdServiceSaveDTO prodServiceSaveDTO = new ProdServiceSaveDTO();
                    prodServiceSaveDTO.setObjuid(prodProdServiceUid);
                    prodServiceSaveDTO.setFacCode("");
                    prodServiceSaveDTO.setProdProductionUid(prodProductionUpdateDTO.getObjuid());
                    prodServiceSaveDTO.setProdBaseServiceUid(prodBaseServiceQueryVO.getObjuid());
                    //prodServiceSaveDTO.setServiceNo(prodBaseServiceQueryVO.getBaseServiceNo());
                    prodServiceSaveDTO.setServiceCode(prodBaseServiceQueryVO.getBaseServiceCode());
                    prodServiceSaveDTO.setServiceName(prodBaseServiceQueryVO.getBaseServiceName());
                    prodServiceSaveDTO.setImportType(prodBaseServiceQueryVO.getImportType());
                    prodServiceSaveDTO.setIsTimeliness(new Integer(prodBaseServiceQueryVO.getIsTimeliness()));
                    prodServiceSaveDTO.setRemark(prodBaseServiceQueryVO.getRemark());
                    prodServiceSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
                    prodServiceSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
                    prodServiceSaveDTO.setDelFlag(0);

                    prodServiceDeleteDTO = new ProdServiceDeleteDTO();
                    prodServiceDeleteDTO.setObjuid(prodProdServiceUid);
                    prodServiceDeleteDTOList.add(prodServiceDeleteDTO);

                    List prodBaseServiceValueQueryVOList = prodBaseServiceValueMapper.queryProdBaseServiceValueByUid2(prodBaseServiceQueryVO.getObjuid());
                    for(int j=0; j<prodBaseServiceValueQueryVOList.size(); j++){
                        ProdBaseServiceValueQueryVO prodBaseServiceValueQueryVO = (ProdBaseServiceValueQueryVO) prodBaseServiceValueQueryVOList.get(j);

                        String prodProdServiceValueUid = StrCP.createObjuid32(UUID.randomUUID().toString());
                        ProdServiceValueSaveDTO prodServiceValueSaveDTO = new ProdServiceValueSaveDTO();
                        prodServiceValueSaveDTO.setObjuid(prodProdServiceValueUid);
                        prodServiceValueSaveDTO.setFacCode("");
                        prodServiceValueSaveDTO.setProdProductionUid(prodProductionUpdateDTO.getObjuid());
                        prodServiceValueSaveDTO.setProdServiceUid(prodProdServiceUid);
                        prodServiceValueSaveDTO.setProdBaseServiceValueUid(prodBaseServiceValueQueryVO.getObjuid());
                        prodServiceValueSaveDTO.setValue(prodBaseServiceValueQueryVO.getUpdateValue());
                        prodServiceValueSaveDTO.setIsDefault(new Integer(prodBaseServiceValueQueryVO.getUpdateIsDefault()));
                        prodServiceValueSaveDTO.setIsHoliday(new Integer(prodBaseServiceValueQueryVO.getUpdateIsHoliday()));
                        prodServiceValueSaveDTO.setAcceptTime(prodBaseServiceValueQueryVO.getUpdateAcceptTime());
                        prodServiceValueSaveDTO.setReplyTime(prodBaseServiceValueQueryVO.getUpdateReplyTime());
                        prodServiceValueSaveDTO.setDeliveryTime(prodBaseServiceValueQueryVO.getUpdateDeliveryTime());
                        prodServiceValueSaveDTO.setArrivalTime(prodBaseServiceValueQueryVO.getUpdateArrivalTime());
                        prodServiceValueSaveDTO.setRemark(prodBaseServiceValueQueryVO.getUpdateRemark());
                        prodServiceValueSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
                        prodServiceValueSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
                        prodServiceValueSaveDTO.setDelFlag(0);

                        prodServiceValueMapper.saveProdServiceValue(prodServiceValueSaveDTO);
                    }

                    prodServiceMapper.saveProdService(prodServiceSaveDTO);
                }else if((!"".equals(prodBaseServiceUpdateDTO.getUpdateObjuid())) && (!prodBaseServiceUpdateDTO.getObjuid().equals(prodBaseServiceUpdateDTO.getUpdateObjuid()))){
                    ProdServiceValueDeleteDTO prodServiceValueDeleteDTO_ = new ProdServiceValueDeleteDTO();
                    prodServiceValueDeleteDTO_.setObjuid(prodBaseServiceUpdateDTO.getUpdateObjuid());
                    prodServiceValueMapper.deleteServiceValue2(prodServiceValueDeleteDTO_);

                    ProdBaseServiceQueryVO prodBaseServiceQueryVO = prodBaseServiceMapper.queryProdBaseServiceByUid2(prodBaseServiceUpdateDTO.getObjuid());
                    ProdServiceUpdateDTO prodServiceUpdateDTO = new ProdServiceUpdateDTO();
                    prodServiceUpdateDTO.setObjuid(prodBaseServiceUpdateDTO.getUpdateObjuid());
                    prodServiceUpdateDTO.setFacCode("");
                    prodServiceUpdateDTO.setProdProductionUid(prodProductionUpdateDTO.getObjuid());
                    prodServiceUpdateDTO.setProdBaseServiceUid(prodBaseServiceQueryVO.getObjuid());
                    //prodServiceUpdateDTO.setServiceNo(prodBaseServiceQueryVO.getBaseServiceNo());
                    prodServiceUpdateDTO.setServiceCode(prodBaseServiceQueryVO.getBaseServiceCode());
                    prodServiceUpdateDTO.setServiceName(prodBaseServiceQueryVO.getBaseServiceName());
                    prodServiceUpdateDTO.setImportType(prodBaseServiceQueryVO.getImportType());
                    prodServiceUpdateDTO.setIsTimeliness(new Integer(prodBaseServiceQueryVO.getIsTimeliness()));
                    prodServiceUpdateDTO.setRemark(prodBaseServiceQueryVO.getRemark());
                    prodServiceUpdateDTO.setMmUserUid(GlobalLoginInfo.getGlobalLoginUser());
                    prodServiceUpdateDTO.setMmTime(GlobalLoginInfo.getGlobalLoginTime());

                    prodServiceDeleteDTO = new ProdServiceDeleteDTO();
                    prodServiceDeleteDTO.setObjuid(prodBaseServiceUpdateDTO.getUpdateObjuid());
                    prodServiceDeleteDTOList.add(prodServiceDeleteDTO);

                    List<ProdBaseServiceValueQueryVO> prodBaseServiceValueQueryVOList = prodBaseServiceValueMapper.queryProdBaseServiceValueByUid2(prodBaseServiceUpdateDTO.getObjuid());
                    for(int j=0; j<prodBaseServiceValueQueryVOList.size(); j++){
                        ProdBaseServiceValueQueryVO prodBaseServiceValueQueryVO = prodBaseServiceValueQueryVOList.get(j);

                        String prodProdServiceValueUid = StrCP.createObjuid32(UUID.randomUUID().toString());
                        ProdServiceValueSaveDTO prodServiceValueSaveDTO = new ProdServiceValueSaveDTO();
                        prodServiceValueSaveDTO.setObjuid(prodProdServiceValueUid);
                        prodServiceValueSaveDTO.setFacCode("");
                        prodServiceValueSaveDTO.setProdProductionUid(prodProductionUpdateDTO.getObjuid());
                        prodServiceValueSaveDTO.setProdServiceUid(prodBaseServiceUpdateDTO.getUpdateObjuid());
                        prodServiceValueSaveDTO.setProdBaseServiceValueUid(prodBaseServiceValueQueryVO.getObjuid());
                        prodServiceValueSaveDTO.setValue(prodBaseServiceValueQueryVO.getUpdateValue());
                        prodServiceValueSaveDTO.setIsDefault(new Integer(prodBaseServiceValueQueryVO.getUpdateIsDefault()));
                        prodServiceValueSaveDTO.setIsHoliday(new Integer(prodBaseServiceValueQueryVO.getUpdateIsHoliday()));
                        prodServiceValueSaveDTO.setAcceptTime(prodBaseServiceValueQueryVO.getUpdateAcceptTime());
                        prodServiceValueSaveDTO.setReplyTime(prodBaseServiceValueQueryVO.getUpdateReplyTime());
                        prodServiceValueSaveDTO.setDeliveryTime(prodBaseServiceValueQueryVO.getUpdateDeliveryTime());
                        prodServiceValueSaveDTO.setArrivalTime(prodBaseServiceValueQueryVO.getUpdateArrivalTime());
                        prodServiceValueSaveDTO.setRemark(prodBaseServiceValueQueryVO.getUpdateRemark());
                        prodServiceValueSaveDTO.setCcUserUid(GlobalLoginInfo.getGlobalLoginUser());
                        prodServiceValueSaveDTO.setCcTime(GlobalLoginInfo.getGlobalLoginTime());
                        prodServiceValueSaveDTO.setDelFlag(0);

                        prodServiceValueMapper.saveProdServiceValue(prodServiceValueSaveDTO);
                    }

                    prodServiceMapper.updateProdService(prodServiceUpdateDTO);
                }else if(prodBaseServiceUpdateDTO.getObjuid().equals(prodBaseServiceUpdateDTO.getUpdateObjuid())){
                    prodServiceDeleteDTO = new ProdServiceDeleteDTO();
                    prodServiceDeleteDTO.setObjuid(prodBaseServiceUpdateDTO.getUpdateObjuid());
                    prodServiceDeleteDTOList.add(prodServiceDeleteDTO);
                }else{
                    continue;
                }
            }

            if (prodServiceDeleteDTOList.size() > 0) {
                int rows = prodServiceMapper.deleteProdService2(prodProductionUpdateDTO.getObjuid(), prodServiceDeleteDTOList);
                prodServiceValueMapper.deleteServiceValue3(prodProductionUpdateDTO.getObjuid(), prodServiceDeleteDTOList);
            }

            int rows = prodProductionMapper.updateProdProduction(prodProductionUpdateDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //删除产品信息表
    @Override
    @Transactional
    public String deleteProdProduction(List<ProdProductionDeleteDTO> prodProductionDeleteDTOList) throws Exception {
        try {
            int rows = 0;
            for (int i = 0; i < prodProductionDeleteDTOList.size(); i++) {
                ProdProductionDeleteDTO prodProductionDeleteDTO = prodProductionDeleteDTOList.get(i);
                rows += prodProductionMapper.deleteProdProduction(prodProductionDeleteDTO.getObjuid());

                prodServiceMapper.deleteProdService(prodProductionDeleteDTO.getObjuid());
                prodServiceValueMapper.deleteServiceValue(prodProductionDeleteDTO.getObjuid());
            }

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //根据主键查询产品及服务项数据
    @Override
    public Map<String, Object> queryProdProductionByUId(String uid) throws Exception {
        ProdProductionQueryVO prodProductionQueryVO = prodProductionMapper.qrodProductionByUid(uid);
        List<ProdServiceQueryVO> prodServiceQueryVOList = prodServiceMapper.queryProdServiceByUid(uid);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("prodProductionQueryVO", prodProductionQueryVO);
        maps.put("prodServiceQueryVOList", prodServiceQueryVOList);
        return maps;
    }

    //根据主键查询产品服务项及参数数据
    @Override
    public Map<String, Object> queryProdServiceByUid(String uid) throws Exception {
        ProdServiceQueryVO prodServiceQueryVO = prodServiceMapper.queryProdServiceByUid2(uid);
        List<ProdServiceValueQueryVO> prodServiceValueQueryVOList = prodServiceValueMapper.queryProdServiceValueByUid(uid);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("prodServiceQueryVO", prodServiceQueryVO);
        maps.put("prodServiceValueQueryVOList", prodServiceValueQueryVOList);
        return maps;
    }

    //根据条件查询产品所有数据
    @Override
    public Map<String, Object> queryProdProductionAll(String typeName, String prodName, int page, int limit) throws Exception {
        int count = prodProductionMapper.queryProdProductionAllCount(typeName, prodName);
        List<ProdProductionQueryVO> prodProductionQueryVOList = prodProductionMapper.queryProdProductionAll(typeName, prodName, (page - 1) * limit, limit);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("count", count);
        maps.put("prodProductionQueryVOList", prodProductionQueryVOList);
        return maps;
    }

    //查询产品类型名称
    @Override
    public List<ProdProductionQueryVO> queryProdProductionProdName() throws Exception {
        List<ProdProductionQueryVO> prodProductionQueryVOList = prodProductionMapper.queryProdProductionProdName();
        return prodProductionQueryVOList;
    }

}
